package com.doctor.adapter;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import com.doctor.comm.ConstConfig;
import com.doctor.comm.ImageHelper;
import com.doctor.ui.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

public class ShowImageAdapter extends BaseAdapter{

	/**
	 * 显示图片的adapter
	 */
	
	private Context context;
	private List<String> beanList;
//	private ImageSize size;
	//图片缓存
	private HashMap<String, SoftReference<Bitmap>> cache = new HashMap<String, SoftReference<Bitmap>>();
	//图片路径前缀
	public static final String PREFIX = "file://";
	
	
	public ShowImageAdapter(){
		
	}
	
	public ShowImageAdapter(Context context, List<String> beanList){
		this.context=context;
		this.beanList=beanList;
//		size=new ImageSize(100, 100);
//		initImageLoader();
	}
	
	
	private void initImageLoader() {
		DisplayImageOptions options=new DisplayImageOptions.Builder()
				.cacheInMemory(true).cacheOnDisc(false).showImageOnFail(R.drawable.no_pic)
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2).build();	
								
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.defaultDisplayImageOptions(options).threadPoolSize(5).build();
						
		ImageLoader.getInstance().init(config);
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beanList.size();
	}

	@Override
	public String getItem(int arg0) {
		// TODO Auto-generated method stub
		return beanList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
	
	public boolean addBean(String path){
		if (beanList.contains(path)) {
			Toast.makeText(context, "不能添加重复的图片！", Toast.LENGTH_SHORT).show();
			return false;
		}
		boolean result=beanList.add(path);
		return result;
	}
	
	public void removeBean(String path){
		beanList.remove(path);
		if (cache.containsKey(path)) {
			Bitmap bitmap = cache.get(path).get();
			if (bitmap != null && !bitmap.isRecycled()) {
				bitmap.recycle();
			}
			cache.remove(path);
		}
		this.notifyDataSetChanged();
	}
	
	
	/**
	 * 手动释放图片占用的内存(不通知adapter数据已清空)
	 */
	public void clear() {
		clear(false);
	}
	
	/**
	 * 手动释放图片占用的内存
	 * @param notify 是否通知adapter数据已清空
	 */
	public void clear(boolean notify){
		Collection<SoftReference<Bitmap>> bitmaps = cache.values();
		for (SoftReference<Bitmap> reference : bitmaps) {
			if (reference != null) {
				Bitmap bitmap = reference.get();
				if (bitmap != null && !bitmap.isRecycled()) {
					bitmap.recycle();
				}
			}
		}
		cache.clear();
		
		if (beanList != null && !beanList.isEmpty()) {
			beanList.clear();
		}
		if (notify) {
			this.notifyDataSetChanged();
		}
	}
	
	public String getAll(){
		int picCount = beanList != null ? beanList.size() : 0;
		if (picCount > 0) {
			StringBuilder builder=new StringBuilder();
			for (int i = 0; i < picCount - 1; i++) {
				builder.append(beanList.get(i));
				builder.append(ConstConfig.IMAGE_SPLIT);  //用来分隔图片路径
			}
			builder.append(beanList.get(picCount - 1));
			return builder.toString();
		}
		return null;
	}
	
	public ArrayList<String> getPics(){
		ArrayList<String> paths=new ArrayList<String>();
		for (String path : beanList) {
			paths.add(path);
		}
		return paths;
	}
	

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (arg1==null) {
			holder=new ViewHolder();
			arg1=View.inflate(context, R.layout.show_image_gridview_item, null);
			holder.img=(ImageView) arg1.findViewById(R.id.img);
			
			arg1.setTag(holder);
		} else {
			holder=(ViewHolder) arg1.getTag();
		}
		
		String pic=beanList.get(arg0);
		holder.img.setTag(pic);
		
		if (cache.containsKey(pic) && cache.get(pic) != null && cache.get(pic).get() != null) {
			//从缓存中取出图片
			holder.img.setImageBitmap(cache.get(pic).get());
		} else {
			//缓存中没有图片则进行加载
			Bitmap bitmap = ImageHelper.getThumbBitmap(pic, 100, 100);
			if (bitmap != null) {
				SoftReference<Bitmap> reference = new SoftReference<Bitmap>(bitmap);
				cache.put(pic, reference);
				String tag = (String) holder.img.getTag();
				if (tag != null && tag.equals(pic)) {
					holder.img.setImageBitmap(bitmap);
				} else {
					holder.img.setImageResource(R.drawable.no_pic);
				}
			} else {
				holder.img.setImageResource(R.drawable.no_pic);
			}
		}
		
			
		
//			ImageLoader.getInstance().displayImage(PREFIX + pic, holder.img);
//			ImageLoader.getInstance().loadImage(PREFIX + pic, size, new ImageLoadingListener() {
//				@Override
//				public void onLoadingStarted(String imageUri, View view) {
//					// TODO Auto-generated method stub
//					
//				}
//				
//				@Override
//				public void onLoadingFailed(String imageUri, View view,
//						FailReason failReason) {
//					// TODO Auto-generated method stub
//					holder.img.setImageResource(R.drawable.no_pic);
//				}
//				
//				@Override
//				public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//					// TODO Auto-generated method stub
//					String tag = (String) holder.img.getTag();
//					if (tag != null && (PREFIX + tag).equals(imageUri) && loadedImage != null) {
//						holder.img.setImageBitmap(loadedImage);
//					} else {
//						holder.img.setImageResource(R.drawable.no_pic);
//					}
//				}
//				
//				@Override
//				public void onLoadingCancelled(String imageUri, View view) {
//					// TODO Auto-generated method stub
//					
//				}
//			});
			
		
		arg1.setTag(R.id.bean_tag, pic);
		return arg1;
	}

	
	private class ViewHolder{
		ImageView img;
	}
	
	

	
}
